<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 800px;
            margin: 20px auto;
        }

        th {
            background: #0094ff;
            color: white;
            font-size: 16px;
            padding: 5px;
            border: 1px solid #000;
        }

        td {
            text-align: center;
            font-size: 16px;
            padding: 5px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <th>编号</th>
            <th>名称</th>
            <th>作者</th>
            <th>操作</th>
            <tr v-if="list.lenth == 0">
                <td colspan="4">列表暂无数据</td>
            </tr>
            <tr v-for="(item,index) in list">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.writer}}</td>
                <td>
                    <button @click="del(index)">删除</button>
                </td>
            </tr>
        </table>
        <div>
            编号：<input type="text" v-model="formData.id"><br>
            名称：<input type="text" v-model="formData.name"><br>
            作者：<input type="text" v-model="formData.writer"><br>
            操作：<button @click="add">添加</button>
        </div>
    </div>

    <script src="../JS/vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    list: [
                        { id: '1', name: '红楼梦', writer: 'Tom' },
                        { id: '2', name: '水浒传', writer: 'Jerry' },
                        { id: '3', name: '三国演义', writer: '李四' },
                        { id: '4', name: '西游记', writer: '张三' },
                    ],
                    formData: {
                        id: '',
                        name: '',
                        writer: '',
                    }
                }
            },
            methods: {
                del(index) {
                    this.list.splice(index, 1);
                },
                add() {
                    let p = { id: this.formData.id, name: this.formData.name, writer: this.formData.writer }
                    this.list.push(p)
                    this.formData.id = ''
                    this.formData.name = ''
                    this.formData.writer = ''
                }
            },
        })
    </script>
</body>

</html>